package com.zw.yzk.kabu.data.repository;

import com.zw.yzk.kabu.data.constant.Constant;
import com.zw.yzk.kabu.data.network.ServiceGenerator;
import com.zw.yzk.kabu.data.network.entity.SkinOrderListEntity;
import com.zw.yzk.kabu.data.network.service.SkinService;
import com.zw.yzk.kabu.data.network.task.SkinListTask;
import com.zw.yzk.kabu.data.network.task.Task;
import rx.Observable;
import rx.functions.Func1;

import javax.inject.Inject;

/**
 * Created by skydar on 2017/4/14.
 */

public class SkinReadyListRepository extends CommonRepository {

    private int totalCount;

    @Inject
    public SkinReadyListRepository() {
        totalCount = SkinListTask.DEFAULT_COUNT_PER_PAGE;
    }


    @Override
    protected Observable connectServer(Task task) {
        SkinService service = ServiceGenerator.createService(SkinService.class);
        SkinListTask skinTask = (SkinListTask) addToken(task);

        if (skinTask.start >= totalCount) {
            SkinOrderListEntity entity = new SkinOrderListEntity();
            //已经全部加载，直接返回空列表
            entity.errCode = Constant.RESULT_OK;
            entity.result.total = totalCount;

            return Observable.just(entity);
        } else {
            return service.getSkinOrderList(
                    skinTask.userId,
                    skinTask.accessToken,
                    skinTask.lastId,
                    skinTask.size, 2);
        }
    }

    @Override
    protected Func1<SkinOrderListEntity, Observable> getServerData() {
        return new Func1<SkinOrderListEntity, Observable>() {
            @Override
            public Observable call(SkinOrderListEntity entity) {
                totalCount = entity.result.total;
                return Observable.just(entity);
            }
        };
    }

}
